

import { Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { DA_SERVICE_TOKEN, ITokenService } from '@delon/auth';

import { NzFormTooltipIcon } from 'ng-zorro-antd/form';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { AccountService } from '../../services/account.service';

@Component({
  selector: 'app-account-components-edit',
  templateUrl: './edit-password.component.html'
})
export class AccountComponentsCenterEditComponent implements OnInit {
  validateForm!: FormGroup;
  record: any;
  count = 0;
  type = 'create';
  isVisibleView = false;
  passwordVisible = false;
  passwordVisible2 = false;
  password: any;
  password2: any;
  interval$: any;
  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control?.value !== this.validateForm?.value?.passWord) {
      return { confirm: true, error: true };
    }
    return {};
  };
  captchaTooltipIcon: NzFormTooltipIcon = {
    type: 'info-circle',
    theme: 'twotone'
  };
  constructor(
    public router: Router,
    private modalRef: NzModalRef,
    private fb: FormBuilder,
    public service: AccountService,
    @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService
  ) {}

  ngOnInit() {
    this.initForm();
  }
  initForm() {
    this.validateForm = this.fb.group({
      passWord: [
        null,
        [
          Validators.required,
          Validators.maxLength(16),
          Validators.minLength(8),
          Validators.pattern('^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z-_]{8,16}$')
        ]
      ],
      passWordTo: [null, [Validators.required, Validators.maxLength(16), Validators.minLength(8), this.confirmationValidator]],
      smsVerifyCode: [null, [Validators.required]]
    });
  }
  destroyModal(): void {
    this.modalRef.destroy();
  }
  getCaptcha(e: MouseEvent): void {
    this.service.request(this.service.$api_get_current_user_smVerification).subscribe(res => {
      // code==503046 弹出网易盾
      if (res && res.code === '1') {
        this.service.msgSrv.success('发送成功');
        e.preventDefault();
        this.codeCountDown();
      } else {
        this.service.msgSrv.success(res.msg);
      }
    });
  }
  save() {
    if (!this.validateForm.valid) {
      this.service.msgSrv.warning('必填项为空或格式错误，请检查！');
      return;
    }
    const params = {
      ...this.validateForm.value
    };
    this.service.request(this.service.$api_set_phoneUpdatePassword, params).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('修改密码成功！');
        this.isVisibleView = true;
        setTimeout(() => {
          this.tokenService.clear();
          this.router.navigate(['/passport/login']);
          this.modalRef.close();
        }, 3000);
      }
    });
  }
  /* code倒计时 */
  codeCountDown() {
    this.count = 59;
    this.interval$ = setInterval(() => {
      this.count -= 1;
      if (this.count <= 0) {
        clearInterval(this.interval$);
      }
    }, 1000);
  }
  handleCancel() {
    this.isVisibleView = false;
  }
  handleOK() {
    this.modalRef.close();
    this.tokenService.clear();
    this.router.navigate(['/passport/login']);
  }
}
